package View;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Point;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.JComponent;
import javax.swing.JPanel;

import Data.Grid;
import Model.Color;
import Model.Game;
import Model.Piece;
import Model.PropertiesUtils;
import Model.Pieces.Bishop;
import Model.Pieces.King;
import Model.Pieces.Knight;
import Model.Pieces.Pawn;
import Model.Pieces.Queen;
import Model.Pieces.Tower;

public class Checkerboard extends JPanel {
	private Square[][] squares;
	public static final int SQUARE_SIZE = 75;
	public static final int SQUARE_NUMBER = 8;
	public static final int CHECKBOARD_WIDTH = Window.FRAME_WIDTH-200;
	public static final int CHECKBOARD_HEIGHT = Window.FRAME_HEIGHT;
	
	public Checkerboard(){
		squares = new Square[SQUARE_NUMBER][SQUARE_NUMBER];
		this.setPreferredSize(new Dimension(CHECKBOARD_WIDTH, CHECKBOARD_HEIGHT));
		this.setLayout(new GridLayout(SQUARE_NUMBER,SQUARE_NUMBER));
		for (int i = 0; i < SQUARE_NUMBER; i++) {
			for (int j = 0; j < SQUARE_NUMBER; j++) {
				Color c = null;
				if(i%2 == 0 && j%2 == 0){
					c = Color.White;
				}else if(i%2 == 1 && j%2 == 0){
					c = Color.Black;
				}else if(i%2 == 0 && j%2 == 1){
					c = Color.Black;
				}else if(i%2 == 1 && j%2 == 1){
					c = Color.White;
				}
				Square s = new Square(SQUARE_SIZE, c);
				squares[i][j] = s;
				this.add(s);
			}
		}
	}
	
	public void reload(char[][] pieces){
		
		for (int i = 0; i < SQUARE_NUMBER; i++) {
			for (int j = 0; j < SQUARE_NUMBER; j++) {
				char c = pieces[i][j];
				String s = PropertiesUtils.imageProperties(Character.toString(c));
				Color team;
				if(Character.toString(c).equals(Character.toString(c).toLowerCase())){
					team = Color.White;
				}else{
					team = Color.Black;
				}				
				if(s != null){
					try {
						squares[j][i].setImage(ImageIO.read(this.getClass().getClassLoader().getResource(s)), team);
					} catch (IOException e) {
						System.err.println("Erreur chargement image");
						e.printStackTrace();
					}
				}else{
					squares[j][i].setImage(null, null);
				}
				squares[j][i].setSelected(false);
				squares[j][i].needHelp(false);
			}
		}
		repaint();
	}
	
	public void help(boolean[][] positions){
		for (int i = 0; i < SQUARE_NUMBER; i++) {
			for (int j = 0; j < SQUARE_NUMBER; j++) {
				squares[j][i].needHelp(positions[i][j]);
			}	
		}
		repaint();
	}
	
	public void selected(Point selected){
		for (int i = 0; i < SQUARE_NUMBER; i++) {
			for (int j = 0; j < SQUARE_NUMBER; j++) {
				if(i == selected.getX() && j == selected.getY()){
					squares[j][i].setSelected(true);
				}else{
					squares[j][i].setSelected(false);
				}
			}	
		}
		repaint();
	}

	public void reload(Grid grid) {
		if(grid.getCheckerboard() != null){
			reload(grid.getCheckerboard());
		}
		if(grid.getHelp() != null){
			help(grid.getHelp());
		}
		if(grid.getSelected() != null){
			selected(grid.getSelected());
		}
	}	
	
}
